package org.ptkparty.client.view.place;

import org.ptkparty.client.activity.PlacesActivity;
import org.ptkparty.client.model.PlaceProxy;

import com.google.gwt.cell.client.AbstractCell;
import com.google.gwt.core.client.Callback;
import com.google.gwt.core.client.GWT;
import com.google.gwt.editor.client.IsEditor;
import com.google.gwt.editor.client.adapters.EditorSource;
import com.google.gwt.editor.client.adapters.ListEditor;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.geolocation.client.Geolocation;
import com.google.gwt.geolocation.client.Position;
import com.google.gwt.geolocation.client.Position.Coordinates;
import com.google.gwt.geolocation.client.PositionError;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.safehtml.shared.SafeHtmlUtils;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.cellview.client.CellList;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.view.client.ListDataProvider;
import com.google.gwt.view.client.SelectionChangeEvent;
import com.google.gwt.view.client.SingleSelectionModel;

public class PlacesView extends Composite implements IsEditor<ListEditor<PlaceProxy, PlaceView>> {

	private static PlacesViewUiBinder uiBinder = GWT.create(PlacesViewUiBinder.class);

	interface PlacesViewUiBinder extends UiBinder<Widget, PlacesView> {
	}

	private PlacesActivity activity;
	private ListDataProvider<PlaceProxy> provider;
	private SingleSelectionModel<PlaceProxy> selectionModel;
	private ListEditor<PlaceProxy, PlaceView> editor;

	@UiField(provided = true)
	CellList<PlaceProxy> placeList;

	@UiField
	SimplePanel placePanel;

	@UiField
	Button addBtn;

	@UiHandler("addBtn")
	void onAddChampionshipBtnClicked(ClickEvent event) {
		final PlaceProxy newPlace = activity.addPlace();

		// Try to get localisation
		if (Geolocation.isSupported()) {
			Geolocation.getIfSupported().getCurrentPosition(new Callback<Position, PositionError>() {

				@Override
				public void onSuccess(Position result) {
					addNewPlace(newPlace, result);
				}

				@Override
				public void onFailure(PositionError reason) {
					addNewPlace(newPlace, null);
				}
			});
		} else {
			addNewPlace(newPlace, null);
		}

	}

	private void addNewPlace(PlaceProxy newPlace, Position result) {
		if (result != null) {
			Coordinates coordinates = result.getCoordinates();
			newPlace.setLatitude(coordinates.getLatitude());
			newPlace.setLongitude(coordinates.getLongitude());
		}

		editor.getList().add(newPlace);
		provider.refresh();
		selectionModel.setSelected(newPlace, true);
	}

	@UiField
	Button deleteBtn;

	@UiHandler("deleteBtn")
	public void onDeleteChampionshipClicked(ClickEvent clickEvent) {
		activity.delete(selectionModel.getSelectedObject());
	}

	@UiField
	Button saveBtn;

	@UiHandler("saveBtn")
	public void onSaveBtnClicked(ClickEvent clickEvent) {
		activity.save();
	}

	public PlacesView() {
		placeList = new CellList<PlaceProxy>(new PlaceCell());

		provider = new ListDataProvider<PlaceProxy>();
		provider.addDataDisplay(placeList);

		editor = ListEditor.of(new EditorSource<PlaceView>() {

			@Override
			public PlaceView create(int index) {
				return new PlaceView();
			}

		});

		selectionModel = new SingleSelectionModel<PlaceProxy>();
		selectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {

			@Override
			public void onSelectionChange(SelectionChangeEvent event) {
				placePanel.clear();
				placePanel.add(editor.getEditors().get(provider.getList().indexOf(selectionModel.getSelectedObject())));
			}
		});
		placeList.setSelectionModel(selectionModel);

		initWidget(uiBinder.createAndBindUi(PlacesView.this));
	}

	static class PlaceCell extends AbstractCell<PlaceProxy> {

		@Override
		public void render(com.google.gwt.cell.client.Cell.Context context, PlaceProxy value, SafeHtmlBuilder sb) {
			sb.append(SafeHtmlUtils.fromString(value.getName()));
		}

	}

	public void setPresenter(PlacesActivity placesActivity) {
		activity = placesActivity;
	}

	public void initProvider() {
		provider.setList(editor.getList());
	}

	public void selectFirstPlace() {
		if (!provider.getList().isEmpty()) {
			selectionModel.setSelected(provider.getList().get(0), true);
		}
	}

	public void removePlaceEditor(PlaceProxy selectedProxy) {
		editor.getList().remove(selectedProxy);
		provider.refresh();
	}

	@Override
	public ListEditor<PlaceProxy, PlaceView> asEditor() {
		return editor;
	}

}
